/*     */ package com.zimbra.cs.index;
/*     */ 
/*     */ import com.google.common.io.Closeables;
/*     */ import com.zimbra.common.account.Key.AccountBy;
/*     */ import com.zimbra.common.io.TcpServerInputStream;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.SoapProtocol;
/*     */ import com.zimbra.common.util.CliUtil;
/*     */ import com.zimbra.common.util.ExceptionToString;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.LogFactory;
/*     */ import com.zimbra.common.util.NetUtil;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.mailbox.MailItem.Type;
/*     */ import com.zimbra.cs.mailbox.MailServiceException;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.mailbox.MailboxIndex;
/*     */ import com.zimbra.cs.mailbox.MailboxManager;
/*     */ import com.zimbra.cs.server.ProtocolHandler;
/*     */ import com.zimbra.cs.server.TcpServer;
/*     */ import java.io.BufferedOutputStream;
/*     */ import java.io.BufferedReader;
/*     */ import java.io.IOException;
/*     */ import java.io.InputStream;
/*     */ import java.io.InputStreamReader;
/*     */ import java.io.OutputStream;
/*     */ import java.io.PrintStream;
/*     */ import java.net.ServerSocket;
/*     */ import java.net.Socket;
/*     */ import java.text.ParseException;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Collection;
/*     */ import java.util.Date;
/*     */ import java.util.EnumSet;
/*     */ import java.util.List;
/*     */ import java.util.Set;
/*     */ import org.apache.log4j.Layout;
/*     */ import org.apache.log4j.Level;
/*     */ import org.apache.log4j.Logger;
/*     */ import org.apache.log4j.PatternLayout;
/*     */ import org.apache.log4j.WriterAppender;
/*     */ import org.apache.lucene.document.DateTools;
/*     */ import org.apache.lucene.document.Document;
/*     */ import org.apache.lucene.document.Field;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public final class IndexEditor
/*     */ {
/*     */   static final int SEARCH_RETURN_CONVERSATIONS = 1;
/*     */   static final int SEARCH_RETURN_MESSAGES = 2;
/*     */   static final int SEARCH_RETURN_DOCUMENTS = 3;
/*  70 */   private static SortBy sortOrder = SortBy.DATE_DESC;
/*  71 */   private BufferedReader inputReader = null;
/*  72 */   private PrintStream outputStream = null;
/*     */   
/*  74 */   private static Log mLog = LogFactory.getLog(IndexEditor.class);
/*     */   
/*     */   public void deleteIndex(int mailboxId) throws ServiceException {
/*  77 */     Mailbox mbox = MailboxManager.getInstance().getMailboxById(mailboxId);
/*     */     try {
/*  79 */       mbox.index.deleteIndex();
/*     */     } catch (IOException e) {
/*  81 */       throw ServiceException.FAILURE("Caught IOException", e);
/*     */     }
/*     */   }
/*     */   
/*     */   public void reIndexAll() {
/*     */     MailboxManager mmgr;
/*     */     try {
/*  88 */       mmgr = MailboxManager.getInstance();
/*     */     } catch (ServiceException e) {
/*  90 */       ZimbraLog.index.error("could not retrieve mailbox manager; aborting reindex", e);
/*  91 */       return;
/*     */     }
/*  93 */     int[] ids = mmgr.getMailboxIds();
/*  94 */     for (int i = 0; i < ids.length; i++) {
/*  95 */       mLog.info("Mailbox " + ids[i] + "\n");
/*     */       try {
/*  97 */         Mailbox mbx = mmgr.getMailboxById(ids[i]);
/*  98 */         mbx.index.startReIndex();
/*     */       } catch (ServiceException e) {
/* 100 */         mLog.info("Exception ReIndexing " + ids[i], e);
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   public void reIndex(int mailboxId) {
/*     */     try {
/* 107 */       Mailbox mbx = MailboxManager.getInstance().getMailboxById(mailboxId);
/* 108 */       mbx.index.startReIndex();
/*     */     } catch (Exception e) {
/* 110 */       this.outputStream.println("Re-index FAILED with " + ExceptionToString.ToString(e));
/*     */     }
/*     */   }
/*     */   
/*     */   public static abstract interface QueryRunner
/*     */   {
/*     */     public abstract ZimbraQueryResults runQuery(String paramString, Set<MailItem.Type> paramSet, SortBy paramSortBy) throws IOException, MailServiceException, ServiceException;
/*     */   }
/*     */   
/*     */   public class SingleQueryRunner implements IndexEditor.QueryRunner {
/*     */     int mMailboxId;
/*     */     
/*     */     SingleQueryRunner(int mailboxId) {
/* 123 */       this.mMailboxId = mailboxId;
/*     */     }
/*     */     
/*     */     public ZimbraQueryResults runQuery(String qstr, Set<MailItem.Type> types, SortBy sortBy)
/*     */       throws IOException, MailServiceException, ServiceException
/*     */     {
/* 129 */       Mailbox mbox = MailboxManager.getInstance().getMailboxById(this.mMailboxId);
/* 130 */       SearchParams params = new SearchParams();
/* 131 */       params.setQueryString(qstr);
/* 132 */       params.setTypes(types);
/* 133 */       params.setSortBy(sortBy);
/* 134 */       params.setOffset(0);
/* 135 */       params.setLimit(100);
/* 136 */       params.setPrefetch(true);
/* 137 */       params.setFetchMode(SearchParams.Fetch.NORMAL);
/* 138 */       ZimbraQuery zq = new ZimbraQuery(null, SoapProtocol.Soap12, mbox, params);
/* 139 */       return zq.execute();
/*     */     }
/*     */   }
/*     */   
/*     */   public void doQuery(QueryRunner runner, boolean dump, int groupBy) throws MailServiceException, IOException, ServiceException
/*     */   {
/*     */     for (;;)
/*     */     {
/* 147 */       this.outputStream.print("Query> ");
/* 148 */       String qstr = this.inputReader.readLine();
/* 149 */       if (qstr.equals("")) {
/* 150 */         return;
/*     */       }
/* 152 */       this.outputStream.println("\n\nTest 1: " + qstr);
/* 153 */       long startTime = System.currentTimeMillis();
/*     */       
/*     */       Set<MailItem.Type> types;
/* 156 */       switch (groupBy) {
/*     */       case 1: 
/* 158 */         types = EnumSet.of(MailItem.Type.CONVERSATION);
/* 159 */         break;
/*     */       case 2: 
/* 161 */         types = EnumSet.of(MailItem.Type.MESSAGE);
/* 162 */         break;
/*     */       default: 
/* 164 */         types = EnumSet.noneOf(MailItem.Type.class);
/*     */       }
/*     */       
/* 167 */       ZimbraQueryResults res = runner.runQuery(qstr, types, sortOrder);
/*     */       try {
/* 169 */         long endTime = System.currentTimeMillis();
/* 170 */         int HITS_PER_PAGE = 20;
/* 171 */         int totalShown = 0;
/*     */         
/* 173 */         res.resetIterator();
/* 174 */         ZimbraHit hit = res.getNext();
/* 175 */         while (hit != null) {
/* 176 */           for (int i = 0; (hit != null) && (i < HITS_PER_PAGE); i++) {
/* 177 */             displayHit(hit, groupBy);
/* 178 */             totalShown++;
/* 179 */             hit = res.getNext();
/*     */           }
/* 181 */           if (hit != null) {
/* 182 */             this.outputStream.print("more (y/n) ? ");
/* 183 */             String line = this.inputReader.readLine();
/* 184 */             if ((line.length() == 0) || (line.charAt(0) == 'n'))
/*     */               break;
/*     */           }
/*     */         }
/* 188 */         this.outputStream.println("Query ran in " + (endTime - startTime) + " ms");
/* 189 */         this.outputStream.println("Displayed a total of " + totalShown + " Hits");
/*     */       } finally {
/* 191 */         Closeables.closeQuietly(res);
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   public void displayHit(ZimbraHit hit, int groupBy) {
/* 197 */     this.outputStream.print("HIT: ");
/* 198 */     if (groupBy == 1) {
/* 199 */       ConversationHit ch = (ConversationHit)hit;
/* 200 */       this.outputStream.println(ch.toString() + " \"  (" + ch.getNumMessageHits() + ")");
/* 201 */       for (MessageHit mh : ch.getMessageHits()) {
/* 202 */         this.outputStream.println("\t" + mh.toString());
/*     */       }
/*     */     }
/* 205 */     else if ((hit instanceof MessageHit)) {
/* 206 */       MessageHit mh = (MessageHit)hit;
/* 207 */       this.outputStream.println(mh.toString());
/* 208 */     } else if ((hit instanceof MessagePartHit)) {
/* 209 */       MessagePartHit mph = (MessagePartHit)hit;
/* 210 */       this.outputStream.println(mph.toString());
/*     */     } else {
/* 212 */       this.outputStream.println(hit.toString());
/*     */     }
/*     */   }
/*     */   
/*     */   public void dumpFields(int mailboxId) throws IOException, ServiceException
/*     */   {}
/*     */   
/*     */   public boolean confirm(String confirmString)
/*     */   {
/* 221 */     this.outputStream.println(confirmString);
/* 222 */     this.outputStream.print("Type YES to confirm: ");
/*     */     try {
/* 224 */       String s = this.inputReader.readLine();
/* 225 */       if (s.equals("YES")) {
/* 226 */         return true;
/*     */       }
/*     */     } catch (Exception e) {
/* 229 */       this.outputStream.print("Caught exception: " + ExceptionToString.ToString(e));
/*     */     }
/*     */     
/* 232 */     return false;
/*     */   }
/*     */   
/*     */   public static String Format(String s, int len) {
/* 236 */     StringBuffer toRet = new StringBuffer(len + 1);
/* 237 */     int curOff = 0;
/* 238 */     if (s.length() < len) {
/* 239 */       for (curOff = 0; curOff < len - s.length(); curOff++) {
/* 240 */         toRet.append(" ");
/*     */       }
/*     */     }
/*     */     
/* 244 */     int sOff = 0;
/* 245 */     for (; curOff < len; curOff++) {
/* 246 */       toRet.append(s.charAt(sOff));
/* 247 */       sOff++;
/*     */     }
/* 249 */     toRet.append("  ");
/* 250 */     return toRet.toString();
/*     */   }
/*     */   
/*     */   public void dumpDocument(Document d, boolean isDeleted) {
/* 254 */     if (isDeleted) {
/* 255 */       this.outputStream.print("DELETED ");
/*     */     }
/*     */     
/*     */ 
/* 259 */     Field f = d.getField("subject");
/* 260 */     String subj; String subj; if (f != null) {
/* 261 */       subj = f.stringValue();
/*     */     } else {
/* 263 */       subj = "MISSING_SUBJECT";
/*     */     }
/* 265 */     f = d.getField("l.mbox_blob_id");
/* 266 */     String blobId; String blobId; if (f != null) {
/* 267 */       blobId = f.stringValue();
/*     */     } else {
/* 269 */       blobId = "MISSING";
/*     */     }
/* 271 */     String dateStr = d.get("l.date");
/* 272 */     if (dateStr == null) {
/* 273 */       dateStr = "";
/*     */     } else {
/*     */       try {
/* 276 */         Date dt = DateTools.stringToDate(dateStr);
/* 277 */         dateStr = dt.toString() + " (" + dt.getTime() + ")";
/*     */       } catch (ParseException e) {
/* 279 */         if (!$assertionsDisabled) throw new AssertionError();
/*     */       }
/*     */     }
/* 282 */     String sizeStr = d.get("l.size");
/* 283 */     if (sizeStr == null) {
/* 284 */       sizeStr = "";
/*     */     }
/* 286 */     String part = d.get("l.partname");
/* 287 */     if (part == null) {
/* 288 */       part = "NO_PART";
/*     */     }
/*     */     
/* 291 */     this.outputStream.println(Format(blobId, 10) + Format(dateStr, 45) + Format(part, 10) + Format(sizeStr, 10) + "\"" + subj + "\"");
/*     */     
/*     */ 
/* 294 */     Field content = d.getField("l.content");
/* 295 */     if (content != null)
/* 296 */       this.outputStream.println("\t" + content.toString()); }
/*     */   
/*     */   public void dumpAll(int mailboxId) throws IOException, ServiceException
/*     */   {}
/*     */   
/*     */   public void dumpDocumentByMailItemId(int mailboxId, int mailItemId) throws ServiceException, IOException
/*     */   {}
/*     */   
/*     */   public void dumpTerms(int mailboxId) throws IOException, ServiceException
/*     */   {}
/*     */   
/*     */   public void getTerms(int mailboxId, String field, int minNum, int maxNum, Collection<?> ret) throws IOException, ServiceException
/*     */   {}
/*     */   
/*     */   public void spanTest(int mailboxId) throws IOException, ServiceException
/*     */   {}
/*     */   
/*     */   public static class TwoTerms implements Comparable<TwoTerms> { public int mCount;
/*     */     
/* 315 */     public int compareTo(TwoTerms other) { if (other.mCount == this.mCount) {
/* 316 */         if (other.s1.equals(this.s1)) {
/* 317 */           return -other.s2.compareTo(this.s2);
/*     */         }
/* 319 */         return -other.s1.compareTo(this.s1);
/*     */       }
/* 321 */       return -(other.mCount - this.mCount);
/*     */     }
/*     */     
/*     */ 
/*     */     public String s1;
/*     */     
/*     */     public String s2;
/*     */   }
/*     */   
/*     */ 
/* 331 */   static IndexEditorTcpServer sTcpServer = null;
/*     */   static IndexEditorProtocolhandler sIndexEditorProtocolHandler;
/* 333 */   static int sPortNo = 7035;
/*     */   static IndexEditorTcpThread tcpServerThread;
/*     */   static Thread sThread;
/*     */   
/*     */   public static void StartTcpEditor() throws ServiceException {
/* 338 */     ServerSocket serverSocket = NetUtil.getTcpServerSocket(null, sPortNo);
/* 339 */     sTcpServer = new IndexEditorTcpServer(3, serverSocket);
/* 340 */     sIndexEditorProtocolHandler = new IndexEditorProtocolhandler(sTcpServer);
/* 341 */     sTcpServer.addActiveHandler(sIndexEditorProtocolHandler);
/* 342 */     sThread = new Thread(new IndexEditorTcpThread(null), "IndexEditor-TcpServer");
/* 343 */     sThread.start();
/*     */   }
/*     */   
/*     */   public static void EndTcpEditor() {
/* 347 */     for (Object cur : inputs) {
/*     */       try {
/* 349 */         if ((cur instanceof InputStream)) {
/* 350 */           ((InputStream)cur).close();
/*     */         } else {
/* 352 */           ((OutputStream)cur).close();
/*     */         }
/*     */       } catch (IOException e) {
/* 355 */         mLog.error("Caught " + ExceptionToString.ToString(e));
/*     */       }
/*     */     }
/*     */     
/* 359 */     if (sTcpServer != null) {
/*     */       try {
/* 361 */         sTcpServer.removeActiveHandler(sIndexEditorProtocolHandler);
/*     */       } finally {
/* 363 */         sTcpServer.stop(0);
/* 364 */         sTcpServer = null;
/*     */       }
/*     */     }
/*     */   }
/*     */   
/* 369 */   public static List<Object> inputs = new ArrayList();
/*     */   private IndexEditorProtocolhandler mHandler;
/*     */   
/*     */   private static class IndexEditorTcpThread implements Runnable {
/*     */     public void run() {
/* 374 */       IndexEditor.sTcpServer.run();
/*     */     }
/*     */   }
/*     */   
/*     */   private static class IndexEditorTcpServer extends TcpServer {
/*     */     IndexEditorTcpServer(int numThreads, ServerSocket serverSocket) {
/* 380 */       super(serverSocket);
/*     */     }
/*     */     
/*     */     public String getName()
/*     */     {
/* 385 */       return "IndexEditorTcpServer";
/*     */     }
/*     */     
/*     */     protected ProtocolHandler newProtocolHandler()
/*     */     {
/* 390 */       return new IndexEditor.IndexEditorProtocolhandler(this);
/*     */     }
/*     */     
/*     */     public int getConfigMaxIdleMilliSeconds()
/*     */     {
/* 395 */       return 0;
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */   private static class IndexEditorProtocolhandler
/*     */     extends ProtocolHandler
/*     */   {
/*     */     private InputStream mInputStream;
/*     */     private OutputStream mOutputStream;
/* 405 */     private IndexEditor mEditor = null;
/*     */     
/* 407 */     private String logLayoutPattern = "%d %-5p [%t] [%x] %c{1} - %m%n";
/*     */     private WriterAppender mAppender;
/*     */     
/*     */     public IndexEditorProtocolhandler(TcpServer server) {
/* 411 */       super();
/*     */     }
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */     protected boolean setupConnection(Socket connection)
/*     */       throws IOException
/*     */     {
/* 422 */       this.mInputStream = new TcpServerInputStream(connection.getInputStream());
/* 423 */       this.mOutputStream = new BufferedOutputStream(connection.getOutputStream());
/*     */       
/* 425 */       IndexEditor.inputs.add(this.mInputStream);
/* 426 */       IndexEditor.inputs.add(this.mOutputStream);
/* 427 */       return true;
/*     */     }
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */     protected boolean authenticate()
/*     */       throws IOException
/*     */     {
/* 437 */       return true;
/*     */     }
/*     */     
/*     */ 
/*     */     public boolean enableLogging()
/*     */     {
/* 443 */       if (this.mAppender == null) {
/* 444 */         Layout layout = new PatternLayout(this.logLayoutPattern);
/* 445 */         this.mAppender = new WriterAppender(layout, this.mOutputStream);
/* 446 */         Logger root = Logger.getRootLogger();
/*     */         
/* 448 */         root.addAppender(this.mAppender);
/* 449 */         return true;
/*     */       }
/* 451 */       return false;
/*     */     }
/*     */     
/*     */     public boolean disableLogging()
/*     */     {
/* 456 */       if (this.mAppender != null) {
/* 457 */         Logger root = Logger.getRootLogger();
/* 458 */         root.removeAppender(this.mAppender);
/* 459 */         this.mAppender = null;
/* 460 */         return true;
/*     */       }
/* 462 */       return false;
/*     */     }
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */     protected boolean processCommand()
/*     */       throws Exception
/*     */     {
/* 473 */       this.mAppender = null;
/*     */       try {
/* 475 */         this.mEditor = new IndexEditor(this);
/* 476 */         this.mEditor.run(this.mInputStream, this.mOutputStream);
/*     */       } finally {
/* 478 */         disableLogging();
/*     */       }
/* 480 */       return false;
/*     */     }
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */     protected void dropConnection()
/*     */     {
/* 489 */       if (this.mInputStream != null) {
/*     */         try {
/* 491 */           this.mInputStream.close();
/*     */         } catch (IOException e) {
/* 493 */           IndexEditor.mLog.warn("While closing output stream, Caught " + ExceptionToString.ToString(e));
/*     */         }
/* 495 */         this.mInputStream = null;
/*     */       }
/* 497 */       if (this.mOutputStream != null) {
/*     */         try {
/* 499 */           this.mOutputStream.close();
/*     */         } catch (IOException e) {
/* 501 */           IndexEditor.mLog.warn("While closing output stream, Caught " + ExceptionToString.ToString(e));
/*     */         }
/* 503 */         this.mOutputStream = null;
/*     */       }
/*     */     }
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */     protected void notifyIdleConnection() {}
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public void run(InputStream input, OutputStream output)
/*     */   {
/* 520 */     BufferedReader reader = new BufferedReader(new InputStreamReader(input));
/* 521 */     PrintStream printer = new PrintStream(output, true);
/* 522 */     run(reader, printer);
/*     */   }
/*     */   
/*     */   public void run() {
/* 526 */     run(new BufferedReader(new InputStreamReader(System.in)), System.out);
/*     */   }
/*     */   
/*     */   public void run(BufferedReader _inputReader, PrintStream _outputStream) {
/* 530 */     this.inputReader = _inputReader;
/* 531 */     this.outputStream = _outputStream;
/*     */     
/* 533 */     String mailboxIdStr = null;
/* 534 */     int mailboxId = 0;
/* 535 */     boolean quit = false;
/*     */     
/* 537 */     while (!quit) {
/*     */       try {
/* 539 */         this.outputStream.print("> ");
/*     */         
/* 541 */         command = null;
/*     */         try {
/* 543 */           command = this.inputReader.readLine();
/*     */         }
/*     */         catch (IOException e) {
/* 546 */           quit = true;
/*     */         }
/*     */       }
/*     */       catch (Exception e)
/*     */       {
/*     */         String command;
/*     */         
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */         QueryRunner runner;
/*     */         
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */         QueryRunner runner;
/*     */         
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */         QueryRunner runner;
/*     */         
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */         QueryRunner runner;
/*     */         
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */         String midStr;
/*     */         
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */         int id;
/*     */         
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 653 */         this.outputStream.println("Caught Exception " + ExceptionToString.ToString(e));
/*     */       }
/* 550 */       if ((command == null) || (command.equals("exit")) || (command.equals("quit"))) {
/* 551 */         quit = true;
/* 552 */       } else if (command.equals("?")) {
/* 553 */         help();
/* 554 */       } else if (command.equals("re-index")) {
/* 555 */         reIndex(mailboxId);
/* 556 */       } else if (command.equals("re-index-all")) {
/* 557 */         reIndexAll();
/* 558 */       } else if (command.equals("re-index-msg")) {
/* 559 */         this.outputStream.print("MSGID> THIS_FUNCTION_CURRENTLY_UNIMPLEMENTED");
/*     */ 
/*     */ 
/*     */       }
/* 563 */       else if (command.equals("sort da")) {
/* 564 */         sortOrder = SortBy.DATE_ASC;
/* 565 */         this.outputStream.println("---->Search order = DATE_ASCENDING");
/* 566 */       } else if (command.equals("sort dd")) {
/* 567 */         sortOrder = SortBy.DATE_DESC;
/* 568 */         this.outputStream.println("---->Search order = DATE_DESCENDING");
/* 569 */       } else if (command.equals("sort sa")) {
/* 570 */         sortOrder = SortBy.SUBJ_ASC;
/* 571 */         this.outputStream.println("---->Search order = SUBJ_ASCENDING");
/* 572 */       } else if (command.equals("sort sd")) {
/* 573 */         sortOrder = SortBy.SUBJ_DESC;
/* 574 */         this.outputStream.println("---->Search order = SUBJ_DESCENDING");
/* 575 */       } else if (command.equals("sort na")) {
/* 576 */         sortOrder = SortBy.NAME_ASC;
/* 577 */         this.outputStream.println("---->Search order = NAME_ASCENDING");
/* 578 */       } else if (command.equals("sort nd")) {
/* 579 */         sortOrder = SortBy.NAME_DESC;
/* 580 */         this.outputStream.println("---->Search order = NAME_DESCENDING");
/* 581 */       } else if (command.equals("sort za")) {
/* 582 */         sortOrder = SortBy.SIZE_ASC;
/* 583 */         this.outputStream.println("---->Search order = SIZE_ASCENDING");
/* 584 */       } else if (command.equals("sort zd")) {
/* 585 */         sortOrder = SortBy.SIZE_DESC;
/* 586 */         this.outputStream.println("---->Search order = SIZE_DESCENDING");
/* 587 */       } else if ((command.equals("q")) || (command.equals("query"))) {
/* 588 */         runner = new SingleQueryRunner(mailboxId);
/* 589 */         doQuery(runner, false, 2);
/* 590 */       } else if ((command.equals("qd")) || (command.equals("querydump"))) {
/* 591 */         runner = new SingleQueryRunner(mailboxId);
/* 592 */         doQuery(runner, true, 3);
/* 593 */       } else if ((command.equals("qc")) || (command.equals("queryconv"))) {
/* 594 */         runner = new SingleQueryRunner(mailboxId);
/* 595 */         doQuery(runner, true, 1);
/*     */ 
/*     */       }
/* 598 */       else if ((command.equals("qp")) || (command.equals("queryconv"))) {
/* 599 */         runner = new SingleQueryRunner(mailboxId);
/* 600 */         doQuery(runner, true, 3);
/* 601 */       } else if (command.equals("mbox")) {
/* 602 */         this.outputStream.print("Enter New Mailbox ID: ");
/* 603 */         mailboxIdStr = this.inputReader.readLine();
/* 604 */         mailboxId = getMailboxIdFromString(mailboxIdStr);
/* 605 */         this.outputStream.println("\tMailboxID set to " + mailboxId);
/* 606 */       } else if (command.equals("fields")) {
/* 607 */         dumpFields(mailboxId);
/* 608 */       } else if (command.equals("terms")) {
/* 609 */         dumpTerms(mailboxId);
/* 610 */       } else if (command.equals("s")) {
/* 611 */         spanTest(mailboxId);
/* 612 */       } else if (command.equals("delete_index")) {
/* 613 */         if (confirm("Are you sure you want to delete the index for mailbox " + mailboxId + "?"))
/*     */         {
/* 615 */           deleteIndex(mailboxId);
/*     */         }
/* 617 */       } else if (command.equals("dumpmi")) {
/* 618 */         this.outputStream.print("Enter Mail-Item-ID:");
/* 619 */         midStr = this.inputReader.readLine();
/*     */         
/* 621 */         if (!midStr.equals("")) {
/* 622 */           id = Integer.parseInt(midStr);
/* 623 */           dumpDocumentByMailItemId(mailboxId, id);
/*     */         }
/* 625 */       } else if (command.equals("dumpall")) {
/* 626 */         dumpAll(mailboxId);
/* 627 */       } else if (command.equals("size")) {
/* 628 */         getSize(mailboxId);
/* 629 */       } else if (command.equals("loglevel")) {
/* 630 */         logLevel();
/* 631 */       } else if (command.equals("snoop")) {
/* 632 */         if (this.mHandler == null) {
/* 633 */           this.outputStream.println("Log Snooping only available in remote mode");
/*     */         }
/* 635 */         else if (this.mHandler.enableLogging()) {
/* 636 */           this.outputStream.println("Log Snooping ENABLED");
/*     */         } else {
/* 638 */           this.outputStream.println("Log Snooping already active.");
/*     */         }
/*     */       }
/* 641 */       else if (command.equals("nosnoop")) {
/* 642 */         if (this.mHandler == null) {
/* 643 */           this.outputStream.println("Log Snooping only available in remote mode");
/*     */         }
/* 645 */         else if (this.mHandler.disableLogging()) {
/* 646 */           this.outputStream.println("Log Snooping DISABLED");
/*     */         } else {
/* 648 */           this.outputStream.println("Log Snooping not active.");
/*     */         }
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   int getMailboxIdFromString(String str)
/*     */     throws ServiceException
/*     */   {
/* 661 */     if ((str != null) && (!str.equals(""))) {
/* 662 */       if (str.indexOf('@') >= 0)
/*     */       {
/* 664 */         Account acct = Provisioning.getInstance().get(Key.AccountBy.name, str);
/* 665 */         Mailbox mbx = MailboxManager.getInstance().getMailboxByAccount(acct);
/* 666 */         return mbx.getId();
/*     */       }
/*     */       
/* 669 */       return Integer.parseInt(str);
/*     */     }
/*     */     
/* 672 */     return 0;
/*     */   }
/*     */   
/*     */   public void logLevel()
/*     */   {
/* 677 */     String logLevel = null;
/*     */     try {
/* 679 */       this.outputStream.print("Enter logging level> ");
/* 680 */       logLevel = this.inputReader.readLine();
/*     */     } catch (Exception e) {
/* 682 */       this.outputStream.print("Caught exception: " + e.toString());
/*     */     }
/*     */     
/* 685 */     Logger root = Logger.getRootLogger();
/*     */     
/* 687 */     if ((logLevel != null) && (!logLevel.equals("")))
/*     */     {
/* 689 */       Level newLevel = null;
/*     */       
/* 691 */       if (logLevel.equalsIgnoreCase("ALL")) {
/* 692 */         newLevel = Level.ALL;
/* 693 */       } else if (logLevel.equalsIgnoreCase("DEBUG")) {
/* 694 */         newLevel = Level.DEBUG;
/* 695 */       } else if (logLevel.equalsIgnoreCase("ERROR")) {
/* 696 */         newLevel = Level.ERROR;
/* 697 */       } else if (logLevel.equalsIgnoreCase("FATAL")) {
/* 698 */         newLevel = Level.FATAL;
/* 699 */       } else if (logLevel.equalsIgnoreCase("INFO")) {
/* 700 */         newLevel = Level.INFO;
/* 701 */       } else if (logLevel.equalsIgnoreCase("OFF")) {
/* 702 */         newLevel = Level.OFF;
/* 703 */       } else if (logLevel.equalsIgnoreCase("WARN")) {
/* 704 */         newLevel = Level.WARN;
/*     */       }
/*     */       
/* 707 */       if (newLevel == null) {
/* 708 */         this.outputStream.println("Unknown level - must be ALL/DEBUG/ERROR/FATAL/INFO/OFF/WARN");
/* 709 */         return;
/*     */       }
/*     */       
/* 712 */       root.setLevel(newLevel);
/*     */     }
/* 714 */     Level cur = root.getLevel();
/* 715 */     this.outputStream.println("Current level is: " + cur);
/*     */   }
/*     */   
/*     */   public IndexEditor(IndexEditorProtocolhandler handler)
/*     */   {
/* 720 */     this.mHandler = handler;
/*     */   }
/*     */   
/* 723 */   public IndexEditor() { this.mHandler = null; }
/*     */   
/*     */   public static void main(String[] args)
/*     */   {
/* 727 */     CliUtil.toolSetup("DEBUG");
/*     */     
/* 729 */     MailboxIndex.startup();
/* 730 */     IndexEditor editor = new IndexEditor();
/* 731 */     editor.run();
/* 732 */     MailboxIndex.shutdown();
/*     */   }
/*     */   
/*     */   void getSize(int mailboxId) throws ServiceException {
/* 736 */     Mailbox mbx = MailboxManager.getInstance().getMailboxById(mailboxId);
/* 737 */     long size = mbx.getSize();
/* 738 */     this.outputStream.println("Mailbox " + mailboxId + " has size " + size + " (" + size / 1024L + "kb)");
/*     */   }
/*     */   
/*     */   void help() {
/* 742 */     this.outputStream.println("\nHELP (updated)");
/* 743 */     this.outputStream.println("----");
/* 744 */     this.outputStream.println("exit-- exit this program");
/* 745 */     this.outputStream.println("re-index -- re-index mailbox from message store");
/* 746 */     this.outputStream.println("re-index-all -- re-index ALL MAILBOXES (!) in the message store");
/* 747 */     this.outputStream.println("re-index-msg -- re-index mailbox from message store");
/* 748 */     this.outputStream.println("sort na|nd|sa|sd|da|dd -- set sort order (name asc/desc, subj asc/desc or date asc/desc)");
/* 749 */     this.outputStream.println("query -- run a query group_by_message");
/* 750 */     this.outputStream.println("queryconv -- run a query group_by_conv");
/*     */     
/* 752 */     this.outputStream.println("mbox -- change mailbox");
/* 753 */     this.outputStream.println("fields -- dump all known fields");
/* 754 */     this.outputStream.println("terms -- dump all known terms for a field");
/*     */     
/* 756 */     this.outputStream.println("delete_index -- deletes the index");
/* 757 */     this.outputStream.println("dumpmi -- dump document by mail_item");
/* 758 */     this.outputStream.println("dumpall -- dump all documents");
/* 759 */     this.outputStream.println("unit -- run unit tests");
/*     */     
/* 761 */     this.outputStream.println("hack -- hacked test code to make a copy of index");
/* 762 */     this.outputStream.println("size -- Return the (uncompressed) size of this mailbox");
/* 763 */     this.outputStream.println("verify -- Verify that all messages in this mailbox are indexed");
/* 764 */     this.outputStream.println("loglevel -- Change the default global logging level (affects all appenders!)");
/* 765 */     this.outputStream.println("snoop -- copy log4j root logger to local output (snoop logs)");
/* 766 */     this.outputStream.println("nosnoop -- stop copying log4j logger");
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/index/IndexEditor.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */